package com.zc.webnews.application;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.zc.webnews.model.Account;
import com.zc.webnews.model.UserInfo;
import com.zc.webnews.utils.SharepreferencesUtilSystemSettings;

import cn.bmob.v3.Bmob;
import cn.sharesdk.framework.ShareSDK;
import cn.smssdk.SMSSDK;

/**
 * Created by Administrator on 2016/11/20.
 */
public class MyApplication extends Application {

    public static Account mAccount = null;
    public static UserInfo mUser = null;
    public boolean isFirstStart = true;
    public boolean isLogin = true;
    /**
     * 获得当前进程号
     *
     * @param context
     * @return
     */
    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {
            SMSSDK.initSDK(this, "192e44ffe19a2", "2e0a987776edd88eafce14dc2279711b");
            Bmob.initialize(this,"7b12a5b95fffd59f5fa5ff7ad6f40b4b");
            ShareSDK.initSDK(this);
            initLoginParams();
            updateLoginParams(mAccount,mUser);
        }

    }

    private void updateLoginParams(Account account,UserInfo user) {
        mAccount = account;
        mUser = user;
        SharepreferencesUtilSystemSettings.putValue(this,Constant.USERACCOUNTCOOKIE,account.getAccount());
        SharepreferencesUtilSystemSettings.putValue(this,Constant.USERPASSWORDCOOKIE,account.getPassword());
        SharepreferencesUtilSystemSettings.putValue(this,Constant.ISLOGIN,user.getIsLogin());
        SharepreferencesUtilSystemSettings.putValue(this,Constant.ID,user.getId());
    }

    private void initLoginParams() {

        String userAccount = SharepreferencesUtilSystemSettings.getValue(this,Constant.USERACCOUNTCOOKIE,"");
        String userPassword = SharepreferencesUtilSystemSettings.getValue(this,Constant.USERPASSWORDCOOKIE,"");
        isFirstStart = SharepreferencesUtilSystemSettings.getValue(this,Constant.ISFIRSTSTART,true);
        isLogin = SharepreferencesUtilSystemSettings.getValue(this,Constant.ISLOGIN,true);
        String id = SharepreferencesUtilSystemSettings.getValue(this,Constant.ID,"-1");
        mUser = new UserInfo();
        mAccount = new Account();
        mAccount.setAccount(userAccount);
        mAccount.setPassword(userPassword);
        mUser.setLogin(isLogin);
        mUser.setId(id);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        System.exit(0);
    }
}
